package com.sam.im.sms.index.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sam.im.greendao.DaoSession;
import com.sam.im.greendao.NumberEntityDao;
import com.sam.im.sms.R;
import com.sam.im.sms.app.MyApp;
import com.sam.im.sms.entity.NumberEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/6/5.
 */
public class BlackListAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<NumberEntity> mList;
    private DaoSession mDaoSession;
    private OnItemClickListener mItemClickListener;

    public BlackListAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
        mDaoSession = MyApp.getDaoSession();
    }

    public void setOnViewItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.card_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NumberEntity model = mList.get(position);
        ((ViewHolder) holder).bind(model);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mItemClickListener.onItemLongClick(position);
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void replaceData(List<NumberEntity> models) {
        mList = models;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        final TextView mTvContent;
        NumberEntity model;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvContent = itemView.findViewById(R.id.tv_content);
        }

        public void bind(NumberEntity model) {
            this.model = model;
            mTvContent.setText(model.getNumber());
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int i);

        void onItemLongClick(int i);
    }
}
